//获取应用实例
var app = getApp();
var appData = app.globalData;

module.exports = {
    //获取用户openid
    getOpenId: function () {
        var _this = this;
        // 判断是否已有openid缓存
        var openid = wx.getStorageSync('openid')
        if (openid) {
            appData.openid = openid;
        } else {
            // 登录
            wx.login({
                success: function (res) {
                    if (res.code) {
                        // api获取微信openid
                        var url = appData.apiUrl + '/user/getopenid';
                        wx.request({
                            url: url,
                            method: 'post',
                            data: {
                                code: res.code
                            },
                            success: function (res) {
                                appData.openid = res.data.openid;
                                // 设置openid缓存
                                wx.setStorageSync('openid', res.data.openid)
                            }
                        })
                    } else {
                    }
                }
            })
        }

    }
}
